package com.github.livebase;

import com.github.livebase.bean.EpgInformation;
import com.github.livebase.database.EpgOperator;
import com.github.livebase.event.Event;
import com.github.livebase.event.EventBus;
import com.github.livebase.event.EventListener;
import com.github.livebase.util.base.CollectionsUtil;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @ClassName EpgUpdator
 * @description: epg信息更新，继承该类实现
 * @author: biao532
 * @create: 2023-12-29 20:06
 * @Version 1.0
 **/
public abstract class EpgInformationUpdater implements EventListener {
    protected Set<String> channelIdSet;
    private EpgOperator operator;
    public EpgInformationUpdater(EpgOperator operator, Set<String> channelIdSet) {
        this.operator = operator;
        this.channelIdSet = channelIdSet;
    }

    protected abstract List<EpgInformation> onDateChange(EpgOperator operator, Date date);

    @Override
    public final void onEvent(Event event){
        if (!Event.isEvent(event, EventBus.EVENT_CHANGE_DATE)) {
            return;
        }
        Date date = event.getAttachObject();
        List<EpgInformation> informations = onDateChange(operator, date);
        if (CollectionsUtil.isEmpty(informations)) {
            return;
        }
        CollectionsUtil.foreach(informations, (i) -> {
            operator.insert(i);
        });
    };
}
